<?php

	//incluimos la conexion a la base de datos
	require_once 'comuns/bbdd.php';
	require_once 'includes/bbdd_functions.php';
		
	
	//incluimos la clase WebService
	require_once 'class/JSON_WebService.php';
	
	//Obtienemos el contenido de POST	
	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	
	//Crear objeto JSON_WebService
	$server = new JSON_WebService($HTTP_RAW_POST_DATA);

	//Registramos los metodos del servicio web
	$server->register("login");
	$server->register("registerUsr");
	$server->register("getObjects");
	$server->register("deleteObject");
	$server->register("addObject");
	$server->register("getAllObjects");
	$server->register("exchangeObjects");


	//Inicializamos el servicio
	$server->start();

	//Definimos los metodos del servicio web
	function login($user){
		
		$sql = 'SELECT * FROM cliente WHERE usr = "'.$user->{'usr'}.'" AND pwd="'.$user->{'pwd'}.'" LIMIT 1';
		$user = leer($sql);
		
		if(isset($user)) return $user;
		else return -1;
	}
	
	function registerUsr($user){
		
		if($user->{'dni'} && $user->{'usr'} && $user->{'pwd'}){
			
			$u = array();
			
			$u['dni'] 	= $user->{'dni'};
			$u['nombre'] 	= $user->{'nombre'};
			$u['apellidos'] = $user->{'apellidos'};
			$u['poblacion'] = $user->{'poblacion'};
			$u['usr'] 		= $user->{'usr'};
			$u['pwd'] 		= $user->{'pwd'};
			$u['fecha'] 	= $user->{'fecha'};
			$u['email'] 	= $user->{'email'};
			
			$id_user = insertar("cliente", $u, true, "RELACIONAL");
			
			if($id_user != '') return 1;
			else return -1;
			
		}else return -1;
	}
	
	function getObjects($user){

		if($user->{'valor_obj'}) $and = 'AND valor_obj IN ('.$user->{'valor_obj'}.','.(($user->{'valor_obj'})+1).','.(($user->{'valor_obj'})-1).')';
		else $and = "";

		$sql = 'SELECT * FROM objeto WHERE cliente = "'.$user->{'usr'}.'" '.$and;

		$objects = leer($sql);

		if(isset($objects)) return $objects;
		else return -1;
	}

	function deleteObject($object){

		if($object->{'id_obj'}){

			eliminar("objeto", "id_obj", $object->{'id_obj'});
			return 1;

		}else return -1;
	}
	
	function addObject($object){

		if($object->{'nombre_obj'} && $object->{'valor_obj'} && $object->{'cliente'}){

			$o = array();
			
			$o['nombre_obj'] 	= $object->{'nombre_obj'};
			$o['img_obj'] 		= $object->{'img_obj'};
			$o['descripcion'] 	= $object->{'descripcion'};
			$o['valor_obj'] 	= $object->{'valor_obj'};
			$o['cliente'] 		= $object->{'cliente'};


			$id_obj = insertar("objeto", $o, true);

			if($id_obj != '') return $id_obj;
			else return -1;
			
		}else return -1;
	}
	
	function getAllObjects($user){
		
		$sql = 'SELECT * FROM objeto WHERE cliente <> "'.$user->{'cliente'}.'"';
		
		$objects = leer($sql);
		 
		if(isset($objects)) return $objects;
		else return -1;
	}
	
	function exchangeObjects($objects){

		if($objects->{'idObj1'} && $objects->{'idObj2'}){

			$sql = 'SELECT cliente FROM objeto WHERE id_obj = "'.$objects->{'idObj1'}.'" LIMIT 1';
			$client1 = leer($sql);
			$client1 = $client1[0]['cliente'];

			$sql = 'SELECT cliente FROM objeto WHERE id_obj = "'.$objects->{'idObj2'}.'" LIMIT 1';
			$client2 = leer($sql);
			$client2 = $client2[0]['cliente'];

			modificar("objeto",array("cliente"=>$client2), "id_obj = ".$objects->{'idObj1'});
			modificar("objeto",array("cliente"=>$client1), "id_obj = ".$objects->{'idObj2'});
			
			return 1;
			
		}else return -1;
	}
	
?>